import { Module } from 'vuex'
import { ILoginState } from '@/store/login/types'
import { IRootState } from '@/store/types'
import { IAccount } from '@/service/login/types'
import {
  accountLoginRequest,
  requestUserInfoById,
  requestUserMenusByRoleId
} from '@/service/login/login'
import localCache from '@/utils/cache'
import router from '@/router'
import { mapMenusToPermissions, mapMenusToRoutes } from '@/utils/map-menus'
import store from '@/store'

const loginModule: Module<ILoginState, IRootState> = {
  namespaced: true,
  state() {
    return {
      token: '',
      userinfo: {},
      userMenus: [],
      userPermissions: []
    }
  },
  getters: {},
  mutations: {
    changeToken(state, token: string) {
      state.token = token
    },
    changeUserInfo(state, userInfo: any) {
      state.userinfo = userInfo
    },
    changeUserMenus(state, userMenus: any) {
      state.userMenus = userMenus

      //userMenus => routes[]
      const routes = mapMenusToRoutes(userMenus)
      //将routes => router.main.children
      routes.forEach((route) => {
        router.addRoute('main', route)
      })
      //获取用户按钮权限
      const permissions = mapMenusToPermissions(userMenus)
      state.userPermissions = permissions
      // console.log(permissions)
    }
  },
  actions: {
    //账号密码登录
    async accountLoginAction({ commit, dispatch }, payload: IAccount) {
      console.log('accountLoginAction被执行', payload)
      //1.实现登录逻辑
      const loginResult = await accountLoginRequest(payload)
      //修改state中的token
      const { id, token } = loginResult.data
      commit('changeToken', token)
      //将token存储在localstorage中
      localCache.setCache('token', token)

      //发送初始化请求，获取完整的role/department,初始化的操作在root的action中
      dispatch('getInitialDataAction', null, { root: true })

      //2.根据用户id请求用户详细信息
      const userInfoResult = await requestUserInfoById(id)
      const userInfo = userInfoResult.data
      commit('changeUserInfo', userInfo)
      //缓存用户信息
      localCache.setCache('userInfo', userInfo)

      //3.请求用户菜单
      const userMenusResult = await requestUserMenusByRoleId(userInfo.role.id)
      const userMenus = userMenusResult.data
      commit('changeUserMenus', userMenus)
      localCache.setCache('userMenus', userMenus)

      //4.跳到首页
      router.push('/main')
    },
    //电话号码登录
    async phoneLoginAction({ commit }, payload: IAccount) {
      // console.log('phoneLoginAction被执行', payload)
    },

    //本地加载
    loadLocalLogin({ commit, dispatch }) {
      // console.log('loadLocalLogin')
      const token = localCache.getCache('token')
      if (token) {
        commit('changeToken', token)
        //发送初始化请求，获取完整的role/department,初始化的操作在root的action中
        dispatch('getInitialDataAction', null, { root: true })
      }

      const userInfo = localCache.getCache('userInfo')
      if (userInfo) {
        commit('changeUserInfo', userInfo)
      }

      const userMenus = localCache.getCache('userMenus')
      if (userMenus) {
        commit('changeUserMenus', userMenus)
      }
    }
  }
}

export default loginModule
